package com.example.mall.controller;

import com.example.mall.dto.request.LoginRequest;
import com.example.mall.dto.response.BaseResponse;
import com.example.mall.dto.response.UserResponse;
import com.example.mall.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("user/")
public class LoginController {

    @Autowired
    private LoginService loginService;


    @PostMapping("login")
    public BaseResponse login(@RequestBody LoginRequest request, HttpSession session)
    {
        UserResponse userResponse = loginService.login(request.getId(), request.getPassword(), request.getFlag(), request.getCheckCode());
        if (userResponse != null)
        {
            session.setAttribute("_login", userResponse);
            return BaseResponse.success(userResponse);
        }
        return BaseResponse.badrequest("用户不存在或密码错误");
    }

    @GetMapping("login")
    public BaseResponse login(HttpSession session)
    {
        UserResponse userResponse = (UserResponse) session.getAttribute("_login");
        if (userResponse != null)
        {
            return BaseResponse.success(userResponse);
        }
        return BaseResponse.badrequest("登录已失效，请重新登录");
    }

    @GetMapping("logout")
    public BaseResponse logout(HttpSession session)
    {
        session.setAttribute("_login", null);
        return BaseResponse.success("退出成功");
    }

    @PostMapping("forgetPassword")
    public BaseResponse forgetPassword(@RequestBody LoginRequest request, HttpSession session)
    {
        loginService.forgetPassword(request.getEmail(), request.getFlag());
        //给邮件发送密码
        return BaseResponse.success();
    }

    @PostMapping("register")
    public BaseResponse Register(@RequestBody LoginRequest request, HttpSession session)
    {
        if (loginService.register(request.getEmail(), request.getPassword(), request.getFlag(), request.getUsername(), request.getIdR(), request.getName(), request.getSex()))
        {
            return BaseResponse.success("注册成功");
        } else
        {
            return BaseResponse.badrequest("该邮箱已被注册");
        }
    }

    @PostMapping("changePassword")
    public BaseResponse changePassword(@RequestBody LoginRequest request, HttpSession session)
    {
        UserResponse userResponse = (UserResponse) session.getAttribute("_login");
        if (userResponse != null)
        {
            if (userResponse.getEmail().equals(request.getEmail()))
            {
                return loginService.changePassword(request.getEmail(), request.getPassword(), request.getFlag(), request.getNewPassword());
            } else
            {
                session.setAttribute("_login", null);
                return BaseResponse.noLogin("未知错误，请重新登录");
            }
        } else
        {
            return BaseResponse.noLogin("登录已失效，请重新登录");
        }
    }

}
